var lastCommentForm = null;

jQuery.fn.removeTags = function()
{
    this.each(function()
    {
        if(jQuery(this).children().length == 0)
        {
            jQuery(this).replaceWith(jQuery(this).text());
        }
        else
        {
            jQuery(this).children().unwrap();
        }
    });
    return this;
};

var markItUpSettings = {
	/* enable later
		{name:'Paragraph', openWith:'<p(!( class="[![Class]!]")!)>', closeWith:'</p>'  },
        {separator:'---------------' },
        {name:'Bold', key:'B', openWith:'(!(<strong>|!|<b>)!)', closeWith:'(!(</strong>|!|</b>)!)' },
		{name:'Italic', key:'I', openWith:'(!(<em>|!|<i>)!)', closeWith:'(!(</em>|!|</i>)!)'  },
		{name:'Stroke through', key:'S', openWith:'<del>', closeWith:'</del>' },
		{separator:'---------------' },
		{name:'Link', key:'L', openWith:'<a href="[![Link:!:http://]!]"(!( title="[![Title]!]")!)>', closeWith:'</a>', placeHolder:'Your text to link...' },
		{separator:'---------------' },
		{name:'Clean', className:'clean', replaceWith:function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") } },	
		{name:'Preview', className:'preview',  call:'preview'}
	*/
	
	onShiftEnter:  			{keepDefault:false, replaceWith:'<br />\n'},
	onCtrlEnter:  			{keepDefault:false, openWith:'\n<p>', closeWith:'</p>'},
	onTab:    				{keepDefault:false, replaceWith:'    '},
	previewTemplatePath:	BASE_URL + '/templates/markitup/preview.html',
	markupSet:  [
	],
	previewParser: function(content) {
		// strip unallowed tags
		temp = $('<div></div>');
		temp.html(content);
		temp.find(":not(p, b, strong, i, em, u, br, del, a)").removeTags();
		return temp.html();
	}
}

$(document).ready(function() {
	$('.markup_textbox').markItUp(markItUpSettings);
	
	$('.reply_to_comment').click(function() {
		var form = $(this).parent().find('form');
		
		if (form.is(':visible')) {
			form.hide();
			lastCommentForm = null;
		} else {
			if (lastCommentForm !== null) lastCommentForm.hide();
			form.show();
		}
		
		return false;
	});
});